package com.hgx.su.common.springboot.web.config;

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * RestTemplate配置类
 *
 * @author hgx
 */
@Configuration
public class RestTemplateConfig {

    /**
     * 配置 RestTemplate
     */
    @Bean
    public RestTemplate restTemplate() {

        return new RestTemplate(httpClientRequestFactory());
    }

    /**
     * httpclient工厂
     */
    public ClientHttpRequestFactory httpClientRequestFactory() {
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory())
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
        //设置整个连接池最大连接数
        connectionManager.setMaxTotal(200);
        //路由是对maxTotal的细分 每个主机的并发
        connectionManager.setDefaultMaxPerRoute(100);


//        RequestConfig requestConfig = RequestConfig.custom()
//                //返回数据的超时时间
//                .setSocketTimeout(60000)
//                //连接上服务器的超时时间
//                .setConnectTimeout(10000)
//                //从连接池中获取连接的超时时间
//                .setConnectionRequestTimeout(3000)
//                .build();
        CloseableHttpClient httpClient = HttpClientBuilder.create()
//                .setDefaultRequestConfig(requestConfig)
                .setConnectionManager(connectionManager)
                //禁用自动重试
                .disableAutomaticRetries()
                .build();


        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        //获取连接超时时间
        factory.setConnectionRequestTimeout(3000);
        //指客户端和服务器建立连接的timeout
        factory.setConnectTimeout(10000);
        //读取数据的超时时间
        factory.setReadTimeout(60000);

        return factory;
    }

    /**
     * okHttp工厂
     */
    public ClientHttpRequestFactory okHttpRequestFactory() {

        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                //不重试
                .retryOnConnectionFailure(false)
                //如果空闲连接超过20个或者keepalive时间大于20分钟，则将该连接清理掉。
                .connectionPool(new ConnectionPool(20, 20, TimeUnit.MINUTES))
                ////读取超时时间
                .readTimeout(60000, TimeUnit.SECONDS)
                //连接超时时间
                .connectTimeout(10000, TimeUnit.SECONDS)
                //写出时间
                .writeTimeout(10000, TimeUnit.SECONDS)
                .hostnameVerifier((hostname, session) -> true)
                // 设置代理
//              .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
                // 拦截器
//                .addInterceptor()
                .build();

        return new OkHttp3ClientHttpRequestFactory(okHttpClient);
    }

}